package com.weitu.base.controller;

import com.weitu.permission.user.entity.User;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;


/**
 * Controller
 *
 * @author wangqingguo
 */
public abstract class BaseController {
    protected static final String BASE_JSP_PATH = "/WEB-INF/jsp/";

    /**
     * 从请求中获取参数
     *
     * @param paramMap 参数Map
     */
    @SuppressWarnings("unchecked")
    protected static void putParametersIntoMap(Map<String, Object> paramMap) {
        User user = getSessionUser();
        paramMap.put("dataUserId", user.getId());
        paramMap.put("dataDeptId", user.getDepartment().getId());
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Enumeration<String> en = request.getParameterNames();
        try {
            while (en.hasMoreElements()) {
                String nms = en.nextElement();
                if (nms.endsWith("[]")) {
                    String[] as = request.getParameterValues(nms);
                    if (as != null && as.length != 0 && !Arrays.toString(as).equals("[]")) {
                        paramMap.put(nms.replace("[]", ""), Arrays.asList(as));
                    }
                } else {
                    String[] as = request.getParameterValues(nms);
                    if (as.length == 1) {
                        paramMap.put(nms, as[0]);
                    } else if (as.length > 1) {
                        paramMap.put(nms, Arrays.asList(as));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得Session 中的用户
     *
     * @return 返回session 中的用户
     */
    protected static User getSessionUser() {

        Session session = SecurityUtils.getSubject().getSession();
        return (User) session.getAttribute("loginUser");
    }

    /**
     * 添加时传入参数
     *
     * @param paramMap 参数
     */
    protected static void getAddBasicFieldMap(Map<String, Object> paramMap) {

        putParametersIntoMap(paramMap);

        String dateTime = DateTimeUtil.formatterDateTime();
        String uuid = IDGenerator.getId();
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        String loginUserDeptId = loginUser.getDepartment().getId();

        //添加操作时间
        paramMap.put("id", uuid);
        paramMap.put("addTime", dateTime);
        paramMap.put("addUserId", loginUserId);
        paramMap.put("addDeptId", loginUserDeptId);
    }

    /**
     * 修改时传入参数
     *
     * @param paramMap 参数
     */
    protected static void getEditBasicFieldMap(Map<String, Object> paramMap) {

        putParametersIntoMap(paramMap);

        String dateTime = DateTimeUtil.formatterDateTime();
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        String loginUserDeptId = loginUser.getDepartment().getId();

        //添加操作时间
        paramMap.put("updateTime", dateTime);
        paramMap.put("updateUserId", loginUserId);
        paramMap.put("updateDeptId", loginUserDeptId);
    }

    /**
     * 转换字符串数字为整数
     *
     * @param value        包含数字的字符串
     * @param defaultValue value为空和null时采用的默认值
     * @return 转换后的整数
     */
    protected static Integer toInt(String value, Integer defaultValue) {
        if (value == null || "".equals(value.trim()))
            return defaultValue;
        value = value.trim();
        return Integer.parseInt(value);
    }

    /**
     * 根据名称获取参数值，有多个值时获取第一个值
     *
     * @param name 参数名称
     * @return 参数值
     */
    protected String getPara(String name) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getParameter(name);
    }

    /**
     * 根据名称获取参数值，有多个值时获取第一个值
     *
     * @param name         参数名称
     * @param defaultValue 当值为空或null时，采取默认值
     * @return 参数值
     */
    protected String getPara(String name, String defaultValue) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String result = request.getParameter(name);
        return result != null && !"".equals(result) ? result : defaultValue;
    }

    /**
     * 根据名称获取整数参数值，有多个值时获取第一个值
     *
     * @param name 参数名称
     * @return 参数值
     */
    protected Integer getParaToInt(String name) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return toInt(request.getParameter(name), null);
    }

    /**
     * 根据名称获取整数参数值，有多个值时获取第一个值
     *
     * @param name         参数名称
     * @param defaultValue 当值为空或null时，采取默认值
     * @return 参数值
     */
    protected Integer getParaToInt(String name, Integer defaultValue) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return toInt(request.getParameter(name), defaultValue);
    }
}
